<template>
  <Dialog
    :show="dialogConfig.show"
    :title="dialogConfig.title"
    :buttons="dialogConfig.buttons"
    width="600px"
    :showCancel="false"
    @close="dialogConfig.show = false"
  >
    <el-form
      :model="formData"
      ref="formDataRef"
      label-width="40px"
      @submit.prevent
    >
      <!--input输入-->
      <el-form-item
        label="日期"
        prop="createTimeRange"
      >
        <el-date-picker
          v-model="formData.createTimeRange"
          type="daterange"
          size="normal"
          range-separator="~"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="YYYY-MM-DD"
          @change="loadRecord"
        >
        </el-date-picker>

      </el-form-item>
    </el-form>
    <div class="data-item">
      <div class="record-type">类型</div>
      <div class="integral">积分</div>
      <div class="create-time">时间</div>
    </div>
    <DataList
      :dataSource="recordListInfo"
      :loading="loading"
      @loadData="loadRecord"
      noDataMsg="暂无相关文章"
    >
      <template #default="{ data }">
        <div class="data-item">
          <div class="record-type">{{ data.operTypeName }}</div>
          <div :class="['integral', data.integral > 0 ? 'add' : 'reduce']">{{ data.integral }}</div>
          <div class="create-time">{{ data.createTime }}</div>
        </div>
      </template>
    </DataList>
  </Dialog>
</template>

<script setup>
import { ref, reactive, getCurrentInstance, nextTick } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { defineExpose } from 'vue';
const { proxy } = getCurrentInstance();
const route = useRoute();
const router = useRouter();
const formData = ref({});
const formDataRef = ref();

const api = {
  loadRecord: '/ucenter/loadUserIntegralRecord'
}

const dialogConfig = reactive({
  show: false,
  title: "个人积分流水",
  buttons: [
    {
      type: "primary",
      text: "返回",
      click: (e) => {
        dialogConfig.show = false
      },
    },
  ],
});

const showRecord = () => {
  dialogConfig.show = true
  nextTick(() => {
    // 重置表单字段
    formDataRef.value.resetFields();
    loadRecord()
  })
}
// 把该方法导出
defineExpose({ showRecord })

const recordListInfo = ref({})
// 是否加载
const loading = ref(true)
const loadRecord = async () => {
  // 开启加载
  loading.value = true
  let params = {
    pageNo: recordListInfo.value.pageNo || 1,
  }
  if (formData.value.createTimeRange) {
    params.createTimeStart = formData.value.createTimeRange[0]
    params.createTimeEnd = formData.value.createTimeRange[1]
  }
  let result = await proxy.Request({
    url: api.loadRecord,
    params: params,
    showLoading: false
  })
  // 关闭加载
  loading.value = false
  if (!result) {
    return
  }
  recordListInfo.value = result.data
}
</script>

<style lang="scss" scoped>
.data-item {
  display: flex;
  border-bottom: 1px solid #ddd;
  padding: 10px 0px;

  .add {
    color: red;
  }

  .reduce {
    color: green;
  }

  .record-type {
    width: 200px;
  }

  .integral {
    width: 30px;
  }

  .create-time {
    text-align: center;
    flex: 1;
  }
}
</style>
